<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-brush"></i> 维修
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button icon="el-icon-brush" type="warning" @click="addVisible = true" round>报修</el-button>
                <el-button icon="el-icon-menu" @click="searchAll" round>查看全小区</el-button>
            </div>
            <el-table
                :data="maintains"
                border
                style="width: 100%;margin-top:28px;">
                <el-table-column
                prop="maintainId"
                label="维修ID"
                width="110">
                </el-table-column>
                <el-table-column
                prop="maintainUser.userAccount"
                label="报修账户名"
                width="180">
                </el-table-column>
                <el-table-column
                prop="maintainUser.userName"
                label="报修用户名">
                </el-table-column>
                <el-table-column
                prop="maintainUser.userPhone"
                label="报修用户手机号">
                </el-table-column>
                <el-table-column
                prop="maintainTitile"
                label="报修标题">
                </el-table-column>
                <el-table-column
                prop="maintainContent"
                label="报修内容">
                </el-table-column>
                <el-table-column
                prop="maintianSite"
                label="报修地点">
                </el-table-column>
                <el-table-column
                prop="maintianState"
                label="报修状态">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.maintianState==0?'danger':(scope.row.maintianState==1?'':'success')">{{scope.row.maintianState==0?'未受理':(scope.row.maintianState==1?'正在处理':'已完成')}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                prop="maintainPrice"
                label="报修价格（￥）">
                </el-table-column>
                <el-table-column label="操作" width="220" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-view"
                            style="color:blue"
                            @click="showComment(scope.$index, scope.row)"
                        >查看评论</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-s-comment"
                            @click="tocomment(scope.$index, scope.row)"
                        >评论</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="del(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 查看全部的弹出框 -->
        <el-dialog title="全部报修" :visible.sync="allVisible" width="60%">
            <el-table
                :data="allmaintains"
                border
                style="width: 100%;margin-top:28px;">
                <el-table-column
                prop="maintainId"
                label="维修ID"
                width="110">
                </el-table-column>
                <el-table-column
                prop="maintainUser.userAccount"
                label="报修账户名"
                width="180">
                </el-table-column>
                <el-table-column
                prop="maintainUser.userName"
                label="报修用户名">
                </el-table-column>
                <el-table-column
                prop="maintainUser.userPhone"
                label="报修用户手机号">
                </el-table-column>
                <el-table-column
                prop="maintainTitile"
                label="报修标题">
                </el-table-column>
                <el-table-column
                prop="maintainContent"
                label="报修内容">
                </el-table-column>
                <el-table-column
                prop="maintianSite"
                label="报修地点">
                </el-table-column>
                <el-table-column
                prop="maintianState"
                label="报修状态">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.maintianState==0?'danger':(scope.row.maintianState==1?'':'success')">{{scope.row.maintianState==0?'未受理':(scope.row.maintianState==1?'正在处理':'已完成')}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                prop="maintainPrice"
                label="报修价格（￥）">
                </el-table-column>
                <el-table-column label="操作" width="220" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-view"
                            style="color:blue"
                            @click="showComment(scope.$index, scope.row)"
                        >查看评论</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>

        <!-- 报修弹出框 -->
        <el-dialog title="报修" :visible.sync="addVisible" width="50%">
            <el-form ref="form" :model="addForm" label-width="100px">
                <el-form-item label="维修标题">
                    <el-input v-model="addForm.maintainTitile"></el-input>
                </el-form-item>
                <el-form-item label="维修内容">
                    <el-input v-model="addForm.maintainContent"></el-input>
                </el-form-item>
                <el-form-item label="维修地点">
                    <el-input v-model="addForm.maintianSite"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="add">维 修</el-button>
            </span>
        </el-dialog>

        <!-- 评论显示弹出框 -->
        <el-dialog title="维修评论" :visible.sync="commentVisible" width="60%">
            <el-table
                :data="comments"
                border
                style="width: 100%;margin-top:28px;">
                <el-table-column
                prop="commentId"
                label="评论ID"
                width="180">
                </el-table-column>
                <el-table-column
                prop="commentContent"
                label="评论内容"
                width="180">
                </el-table-column>
                <el-table-column
                prop="commentScore"
                label="服务评分"
                width="180">
                    <template slot-scope="scope">
                        <el-rate
                        disabled
                        v-model="scope.row.commentScore">
                        </el-rate>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>

        <!-- 发布评论弹出框 -->
        <el-dialog title="发布维修评论" :visible.sync="tocommentVisible" width="40%">
            <el-form ref="form" :model="commentForm" label-width="100px">
                <el-form-item label="维修内容">
                    <el-input v-model="commentForm.commentContent"></el-input>
                </el-form-item>
                <el-form-item label="服务满意评分">
                    <el-rate allow-half v-model="commentForm.commentScore"></el-rate>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="tocommentVisible = false">取 消</el-button>
                <el-button type="primary" @click="usercomment">评 论</el-button>
            </span>
        </el-dialog>

    </div>
</template>
<script>
import {getHouses,maintainAll,maintainSelf,delUserMaintain,addUserMaintain,getComments,getUserComments,usercomment} from '@/api'

export default {
    data () {
        return {
            maintains: [],
            comments: [],
            allmaintains: [],
            id: '',
            houses: [],
            addVisible: false,
            addForm: {},
            updateForm: {},
            commentForm: {},
            updateVisible: false,
            allVisible: false,
            commentVisible: false,
            tocommentVisible: false,
        }
    },
    methods: {
        searchAll(){
            this.allVisible = true
            maintainAll().then(res => {
                this.allmaintains = res.data
            })
        },
        tocomment(index,row){
            this.idx = index
            this.tocommentVisible = true
        },
        usercomment(){
            let fromData = new FormData()
            fromData.append('commentMaintainId', this.maintains[this.idx].maintainId)
            fromData.append('commentContent', this.commentForm.commentContent)
            fromData.append('commentScore', this.commentForm.commentScore)
            usercomment(fromData).then(res => {
                if (res.code === 0) {
                    this.tocommentVisible = false
                    this.$message({
                        type: 'success',
                        message: '评论成功'
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: '评论失败'
                    })
                }
            }) 
        },
        add(){
            let fromData = new FormData()
            fromData.append('maintainTitile', this.addForm.maintainTitile)
            fromData.append('maintainContent', this.addForm.maintainContent)
            fromData.append('maintianSite', this.addForm.maintianSite)
            addUserMaintain(fromData).then(res => {
                if (res.code === 0) {
                    this.addVisible = false
                    this.$message({
                        type: 'success',
                        message: '添加成功'
                    })
                    this.searchSelf()
                } else {
                    this.$message({
                        type: 'error',
                        message: '添加失败'
                    })
                }
            })
        },
        searchSelf(){
            maintainSelf().then(res => {
                this.maintains = res.data
            })
        },
        showComment(index,row){
            this.commentVisible = true
            getUserComments(row.maintainId).then(res =>{
                this.comments = res.data
            })
        },
        getHouses(){
            getHouses().then(res => {
                this.houses = res.data
            })
        },
        getMaintains(){
            getMaintains(this.id).then(res => {
                this.maintains = res.data
            })
        },
        changeHouse(){
            this.getMaintains()
        },
        del (index, row) {
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                let fromData = new FormData()
                fromData.append('id', this.maintains[index].maintainId)
                delUserMaintain(fromData).then(res => {
                    if (res.code === 0) {
                        this.$message({
                            type: 'success',
                            message: '删除成功'
                        })
                        this.maintains.splice(index, 1)
                    } else {
                        this.$message({
                            type: 'error',
                            message: '删除失败'
                        })
                    }
                })
            })
            .catch(() => {});
        }
    },
    mounted () {
        this.searchSelf()
    }
}
</script>
<style scoped>
.red {
    color: red;
}
</style>